<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/7/4
 * Time: 14:40
 */

namespace app\admin\controller;

use think\Controller;
use think\Request;

class base extends Controller
{

    protected $model;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        if (!$this->getLoginUser()) {
            return $this->redirect('pub/login');
        }
        $this->assign('user', $this->getLoginUser());
    }

    public function _initialize()
    {
        $this->model = model($this->request->controller());
    }

    public function getLoginUser()
    {
        $user = session('user', '', 'admin');
        if ($user && $user->id) {
            return $user;
        }
        return false;
    }


    public function index()
    {
        $list = $this->model->page_index($this->datas)->appends($this->request->param());
        $this->assign('num', $this->model->where($this->datas)->count());
        $this->assign('list', $list);
        $this->assign('addcid', $this->request->param('cid'));
        return $this->fetch();
    }

    /**
     * 添加
     */
    public function add()
    {
        return $this->fetch('edit',
            ['addcid' => $this->request->param('addcid'),]
        );
    }

    /**
     * 编辑
     */
    public function edit($id)
    {
        if (intval($id) < 1) {
            $this->error('数据错误');
        }
        $data = $this->model->get($id);
        $this->assign('data', $data);
        return $this->fetch();
    }


    /**
     * 修改
     */
    public function update($data)
    {
        $res = $this->model->allowField(true)->save($data, ['id' => intval($data['id'])]);
        if ($res) {
            return ajax_info('修改成功！','u');
        } else {
            return ajax_info('没有修改！');
        }
    }

    /**
     * 删除
     */
    public function del($id)
    {
        $re = $this->model->del($id);
        if ($re) {
            deldir(RUNTIME_PATH);

            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }


    /**
     * 排序
     */
    public function sort()
    {
        if ($this->request->isAjax()) {
            $result = $this->model->save(['sort' => $this->request->post('sort')], ['id' => $this->request->post('id')]);
            if ($result) {
                $this->success('修改成功！');
            } else {
                $this->error('没有修改！');
            }
        }
    }

    /**
     * 恢复
     */
    public function resume()
    {
        if ($this->request->isAjax()) {
            $result = $this->model->save(['status' => '1'], ['id' => $this->request->post('id')]);
            if ($result) {
                $this->success('修改成功！');
            } else {
                $this->error('没有修改！');
            }
        }
    }

    /**
     * 禁用
     */
    public function forbid()
    {
        if ($this->request->isAjax()) {
            $result = $this->model->save(['status' => '0'], ['id' => $this->request->post('id')]);
            if ($result) {
                $this->success('修改成功！');
            } else {
                $this->error('没有修改！');
            }
        }
    }


    /**
     * 推荐
     */
    public function Rrecommended()
    {
        if ($this->request->isAjax()) {
            $result = $this->model->save(['tuijian' => '1'], ['id' => $this->request->post('id')]);
            if ($result) {
                $this->success('修改成功！');
            } else {
                $this->error('没有修改！');
            }
        }
    }

    /**
     * 不推荐
     */
    public function NotRrecommended()
    {
        if ($this->request->isAjax()) {
            $result = $this->model->save(['tuijian' => '0'], ['id' => $this->request->post('id')]);
            if ($result) {
                $this->success('修改成功！');
            } else {
                $this->error('没有修改！');
            }
        }
    }

}